﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class CreateNewAcct : Form
    {
        public CreateNewAcct()
        {
            InitializeComponent();

            //Random generate password
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            var result = new string(Enumerable.Repeat(chars, 6).Select(s => s[random.Next(s.Length)]).ToArray());
            lblPassword.Text = result.ToString();

        }

        /*insert new account*/
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (txbFirstName.Text == String.Empty || txbLastName.Text == String.Empty || cbxPosition.Text == String.Empty || cmbAssignedDuty.Text == String.Empty)
            {
                MessageBox.Show("Pleasae fill up all required information.");
                lblID.Text = "Failed !";
            }
            else
            {
                AccountDBManager accdb = new AccountDBManager();
                List<Account> acctList = accdb.GetAllAccounts();
                Account a = acctList.Last();

                Account ac = new Account();
                ac.staffId = NxtKeyCode(a.staffId);
                ac.staffPasswd = lblPassword.Text;
                ac.firstName = txbFirstName.Text;
                ac.lastName = txbLastName.Text;
                ac.staffPosition = cbxPosition.Text;
                if (rbnMale.Checked == true)
                {
                    ac.gender = rbnMale.Text.ToString();
                }
                else if (rbnFemale.Checked == true)
                {
                    ac.gender = rbnFemale.Text.ToString();
                }
                ac.dob = dtpDOB.Value;
                mtbContact.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
                ac.contactNo = Convert.ToInt64(mtbContact.Text);
                ac.acctType = cbxAcctType.SelectedItem.ToString();
                mtbAcctNo.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
                ac.acctNo = mtbAcctNo.Text;
                ac.address = txbAddress.Text;
                ac.dutyType = cmbAssignedDuty.SelectedItem.ToString();

                AccountDBManager.insertAccount(ac);
                lblID.Text = ac.staffId + "  " + "Success!";
            }
            

        }

        /*reset account information*/
        private void btnReset_Click(object sender, EventArgs e)
        {
            
            txbFirstName.Text=null;
            txbLastName.Text=null;
            cbxPosition.SelectedText=null;
            rbnMale.Checked=false;
            rbnFemale.Checked=false;
            mtbContact.Text= null; ;
            txbAddress.Text=null;;
            cbxAcctType.SelectedText=null;
            mtbAcctNo.Text = null; 
            cmbAssignedDuty.SelectedText=null;
            
        }

        #region AlphanumericIncrementForBookingID
        public string NxtKeyCode(string KeyCode)
        {
            byte[] ASCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode);
            int StringLength = ASCIIValues.Length;
            bool isAllZed = true;
            bool isAllNine = true;
            //Check if all has ZZZ.... then do nothing just return empty string.

            for (int i = 0; i < StringLength - 1; i++)
            {
                if (ASCIIValues[i] != 90)
                {
                    isAllZed = false;
                    break;
                }
            }
            if (isAllZed && ASCIIValues[StringLength - 1] == 57)
            {
                ASCIIValues[StringLength - 1] = 64;
            }

            // Check if all has 999... then make it A0
            for (int i = 0; i < StringLength; i++)
            {
                if (ASCIIValues[i] != 57)
                {
                    isAllNine = false;
                    break;
                }
            }
            if (isAllNine)
            {
                ASCIIValues[StringLength - 1] = 47;
                ASCIIValues[0] = 65;
                for (int i = 1; i < StringLength - 1; i++)
                {
                    ASCIIValues[i] = 48;
                }
            }


            for (int i = StringLength; i > 0; i--)
            {
                if (i - StringLength == 0)
                {
                    ASCIIValues[i - 1] += 1;
                }
                if (ASCIIValues[i - 1] == 58)
                {
                    ASCIIValues[i - 1] = 48;
                    if (i - 2 == -1)
                    {
                        break;
                    }
                    ASCIIValues[i - 2] += 1;
                }
                else if (ASCIIValues[i - 1] == 91)
                {
                    ASCIIValues[i - 1] = 65;
                    if (i - 2 == -1)
                    {
                        break;
                    }
                    ASCIIValues[i - 2] += 1;

                }
                else
                {
                    break;
                }

            }
            KeyCode = ASCIIEncoding.ASCII.GetString(ASCIIValues);
            return KeyCode;
        }
        #endregion AlphanumericIncrementForBookingID

        #region toolStrip
        UpdateAcct updateAcctChild;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (updateAcctChild == null)
            {
                updateAcctChild = new UpdateAcct();
                updateAcctChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                updateAcctChild.MdiParent = this.MdiParent;
                // Display the form.
                updateAcctChild.Show();
                updateAcctChild.Activate();
            }
        }

        CreateNewAcct createAcctChild;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (createAcctChild == null)
            {
                createAcctChild = new CreateNewAcct();
                createAcctChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                createAcctChild.MdiParent = this.MdiParent;
                // Display the form.
                createAcctChild.Show();
                createAcctChild.Activate();
            }
        }

        DeleteAcct deleteAcct;
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (deleteAcct == null)
            {
                deleteAcct = new DeleteAcct();
                deleteAcct.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                deleteAcct.MdiParent = this.MdiParent;
                // Display the form.
                deleteAcct.Show();
                deleteAcct.Activate();
            }
        }
        #endregion toolStrip

        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                updateAcctChild = null;
                createAcctChild = null;
                deleteAcct = null;
            }
        }
        #endregion CloseAllForms
    }
}
